//调用poi类信接口
//清除地图上的marker

function clearOverlays(overlays) {
    var overlay;
    while (overlay = overlays.pop()) {
        overlay.setMap(null);
    }
}
function geolocation_localcity(id) {
    //console.log(132146);
    var objS  = document.getElementById(id);
    var index=objS.selectedIndex ;
    localcity = objS.options[index].value;

    console.log(11,localcity);
    clearOverlays(markers);
    searchService.search(localcity);
    //citylocation.searchCityByName(localcity);

}
function searchKeyword() {
    var localcity = document.getElementById("keyword").value;
    console.log(localcity);
    clearOverlays(markers);
    searchService.search(localcity);
    //citylocation.searchCityByName(localcity);


}

$(function(){
    locat()
})
function locat(){
    var data=[];
    $.ajax({
        url: "{:url('store/getIpWsLocation')}",
        type: 'post',
        async: true,
        data: {},
        success: function (data) {
            console.log(22,data);
            var data=data.location;
            init(data)
        }
    })
    //console.log(data);
}


var searchService,geocoder,citylocation, map, markers = [];
var init = function (data) {

    var center = new qq.maps.LatLng(data.lat,data.lng);
    map = new qq.maps.Map(document.getElementById('container'), {
        center: center,
        zoom: 13,
        panControl: true,
        zoomControl: true,
        zoomControlOptions: {
            style: qq.maps.ZoomControlStyle.SMALL
        }
    });

    ///
    var city = document.getElementById("address");
    citylocation = new qq.maps.CityService({
        complete : function(results){
            map.setCenter(results.detail.latLng);
            city.style.display = 'inline';
            city.innerHTML = '所在位置: ' + results.detail.latLng;
            if (marker != null) {
                marker.setMap(null);
            }
            console.log(city);
            marker = new qq.maps.Marker({
                map: map,
                position: results.detail.latLng
            });
        }
    });
    //
    var latlngBounds = new qq.maps.LatLngBounds();
    //调用Poi检索类
    searchService = new qq.maps.SearchService({
        complete: function (results) {
            var pois = results.detail.pois;
            for (var i = 0, l = pois.length; i < l; i++) {
                var poi = pois[i];
                latlngBounds.extend(poi.latLng);
                var marker = new qq.maps.Marker({
                    map: map,
                    position: poi.latLng
                });
                marker.setTitle(i + 1);
                markers.push(marker);
            }
            map.fitBounds(latlngBounds);
        }
    });

    var marker = new qq.maps.Marker({
        map: map,
        position: center
    });

    var infoWin = new qq.maps.InfoWindow({
        map: map
    });
    qq.maps.event.addListener(
        map,
        'click',
        function (event) {
            $("#latitude").val(event.latLng.getLat());
            $("#longitude").val(event.latLng.getLng());
            $.ajax({
                url: "{:url('store/getaddress')}",
                type: 'post',
                async: true,
                data: {
                    lat: event.latLng.getLat(),
                    lng: event.latLng.getLng()
                },
                success: function (data) {
                    infoWin.open();
                    infoWin.setContent('<div style="text-align:center;white-space:nowrap;margin:10px;">' + data.address + '</div>');
                    infoWin.setPosition(event.latLng);
                    $("#address").val(data.address);
                    $("#province").val(data.address_component.province);
                    $("#city").val(data.address_component.city);
                    $("#district").val(data.address_component.district);
                    $("#street").val(data.address_component.street);
                    $("#street_number").val(data.address_component.street_number);
                },
                error: function () {
                    alert("获取地址失败");
                }
            })
        }
    );
}